package servlet;

import java.io.*;
import java.sql.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import util.*;
import bll.*;
import model.*;
import dal.*;

/**
 * Servlet implementation class CompanyServlet
 */
@WebServlet(urlPatterns = { "/Employee", "/employee", "/EmployeeServlet", "/employeeServlet" }, initParams = { @WebInitParam(name = "mode", value = "") })
public class EmployeeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * Default constructor.
	 */
	public EmployeeServlet() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");

		String mode = request.getParameter("mode");

		if (mode.equalsIgnoreCase("new")) {

			if (RightUtil.<EmployeeModel> checkInsertRight()) {

				// TODO : MORE CODE HERE

				getServletContext().getRequestDispatcher("/employeeNew.jsp").forward(request, response);
			} else {
				// TODO no right
			}
		} else if (mode.equalsIgnoreCase("edit")) {

			EmployeeModel query = new EmployeeModel();
			String id = request.getParameter("id");
			query.setId(Integer.parseInt(id));

			GenericDal<EmployeeModel, EmployeeModelExt> dal = new GenericDal<EmployeeModel, EmployeeModelExt>(new EmployeeModel());

			EmployeeModel obj = null;
			try {
				obj = dal.select(query);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			if (obj != null) {

				// TODO : MORE CODE HERE

				request.setAttribute("employee", obj);
				getServletContext().getRequestDispatcher("/employeeEdit.jsp").forward(request, response);

			} else {
				// TODO : data not found
			}
		} else if (mode.equalsIgnoreCase("view")) {

			EmployeeModel query = new EmployeeModel();
			String id = request.getParameter("id");
			query.setId(Integer.parseInt(id));

			GenericDal<EmployeeModel, EmployeeModelExt> dal = new GenericDal<EmployeeModel, EmployeeModelExt>(new EmployeeModel());

			EmployeeModel obj = null;
			try {
				obj = dal.select(query);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			if (obj != null) {
				// TODO : LOAD MASTER DATA HERE
				// It's specific code that cannot be generated

				request.setAttribute("employee", obj);
				getServletContext().getRequestDispatcher("/employeeView.jsp").forward(request, response);

			} else {
				// TODO : data not found
			}
		} else if (mode.equalsIgnoreCase("quickInsert")) {
			response.setContentType("text/xml; charset=UTF-8");
			EmployeeModel obj = new EmployeeModel();

			// TODO set name here

			GenericDal<EmployeeModel, EmployeeModelExt> dal = new GenericDal<EmployeeModel, EmployeeModelExt>(new EmployeeModel());

			int res = 0;

			try {
				String strName = request.getParameter("name");
				strName = new String(strName.getBytes("ISO-8859-1"), "UTF-8");

				obj.setName(strName);
				res = dal.insert(obj);
				PrintWriter out = response.getWriter();
				// TODO: check failed inserting case
				if (res > 0) {
					String resultXML = CommonUtil.createAjaxResultXML("employee", "" + res);
					out.println(resultXML);
				}
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			// TODO : return value

		} else if (mode.equalsIgnoreCase("delete")) {

			EmployeeModel query = new EmployeeModel();
			String id = request.getParameter("id");
			query.setId(Integer.parseInt(id));

			GenericDal<EmployeeModel, EmployeeModelExt> dal = new GenericDal<EmployeeModel, EmployeeModelExt>(new EmployeeModel());

			int res = Integer.MIN_VALUE;
			try {
				res = dal.delete(query);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			if (res == 0) {
				// TODO success
				response.sendRedirect("employeeList.jsp");
			} else {
				// TODO fail
				PrintWriter out = response.getWriter();
				out.println("Cannot delete!");
			}
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");

		String mode = request.getParameter("mode");

		if (mode.equalsIgnoreCase("insert")) {

			EmployeeModel obj = new EmployeeModel();

			String code = request.getParameter("code");
			obj.setCode(code);

			String note = request.getParameter("note");
			obj.setNote(note);

			String user_name = request.getParameter("user_name");
			obj.setUserName(user_name);
			/*
			 * String id_person = request.getParameter("id_person"); if
			 * (id_person != null && !id_person.isEmpty()) {
			 * obj.setIdPerson(Integer.parseInt(id_person)); }
			 * 
			 * String status = request.getParameter("status"); if (status !=
			 * null && !status.isEmpty()) {
			 * obj.setStatus(Integer.parseInt(status)); }
			 * 
			 * String is_draft = request.getParameter("is_draft");
			 * obj.setIsDraft(is_draft.equalsIgnoreCase("true") ||
			 * is_draft.equalsIgnoreCase("1"));
			 * 
			 * String notification = request.getParameter("notification"); if
			 * (notification != null && !notification.isEmpty()) {
			 * obj.setNotification(Integer.parseInt(notification)); }
			 */

			String name = request.getParameter("name");
			obj.setName(name);

			obj.setCreator(SessionUtil.getCurrentUserID());

			if (CommonBll.checkInputs(obj)) {
				GenericDal<EmployeeModel, EmployeeModelExt> dal = new GenericDal<EmployeeModel, EmployeeModelExt>(new EmployeeModel());

				int res = 0;
				try {
					res = dal.insert(obj);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				if (res > 0) {
					// TODO : success, redirect to somewhere
					
					response.sendRedirect("employeeNew.jsp");
				} else {
					// TODO : processing error here
				}
			} else {

				// invalid input

				request.setAttribute("employee", obj);
				getServletContext().getRequestDispatcher("/employeeInput.jsp").forward(request, response);

			}
		} else if (mode.equalsIgnoreCase("update")) {

			EmployeeModel query = new EmployeeModel();
			String id = request.getParameter("id");
			query.setId(Integer.parseInt(id));

			GenericDal<EmployeeModel, EmployeeModelExt> dal = new GenericDal<EmployeeModel, EmployeeModelExt>(new EmployeeModel());

			EmployeeModel obj = null;
			try {
				obj = dal.select(query);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			if (obj != null) {

				String code = request.getParameter("code");
				obj.setCode(code);

				String note = request.getParameter("note");
				obj.setNote(note);

				String user_name = request.getParameter("user_name");
				obj.setUserName(user_name);

				/*
				 * String id_person = request.getParameter("id_person"); if
				 * (id_person != null && !id_person.isEmpty()) {
				 * obj.setIdPerson(Integer.parseInt(id_person)); }
				 * 
				 * 
				 * String status = request.getParameter("status"); if (status !=
				 * null && !status.isEmpty()) {
				 * obj.setStatus(Integer.parseInt(status)); }
				 * 
				 * String is_draft = request.getParameter("is_draft");
				 * obj.setIsDraft(is_draft.equalsIgnoreCase("true") ||
				 * is_draft.equalsIgnoreCase("1"));
				 * 
				 * 
				 * String notification = request.getParameter("notification");
				 * if (notification != null && !notification.isEmpty()) {
				 * obj.setNotification(Integer.parseInt(notification)); }
				 */

				String name = request.getParameter("name");
				obj.setName(name);

				if (CommonBll.checkInputs(obj)) {

					int res = Integer.MIN_VALUE;
					try {
						res = dal.update(obj);
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (ClassNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

					if (res == 0) {
						// TODO success
						response.sendRedirect("employeeList.jsp");
					} else {
						// TODO error
					}
				} else {
					request.setAttribute("employee", obj);
					getServletContext().getRequestDispatcher("/employeeList.jsp").forward(request, response);
				}
			} else {
				// TODO data not found
			}
		} else if (mode.equalsIgnoreCase("quickInsert")) {
			response.setContentType("text/xml; charset=UTF-8");
			EmployeeModel obj = new EmployeeModel();

			// TODO set name here

			GenericDal<EmployeeModel, EmployeeModelExt> dal = new GenericDal<EmployeeModel, EmployeeModelExt>(new EmployeeModel());

			int res = 0;

			try {
				String strName = request.getParameter("name");
				strName = new String(strName.getBytes("ISO-8859-1"), "UTF-8");

				obj.setName(strName);
				res = dal.insert(obj);
				PrintWriter out = response.getWriter();
				// TODO: check failed inserting case
				if (res > 0) {
					String resultXML = CommonUtil.createAjaxResultXML("employee", "" + res);
					out.println(resultXML);
				}
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			// TODO : return value

		}
	}

}